package com.yk.i_wms.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.model.PdtFeederDetail;
import com.yk.i_wms.service.IPdtFeederDetailService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import com.yk.i_wms.vo.PdtFeederDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 料站表详情
 *
 * @author hbq
 * @date 2021-05-12 13:52:57
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "料站表详情")
@RequestMapping("pdt")
public class PdtFeederDetailController {
    @Autowired
    private IPdtFeederDetailService pdtFeederDetailService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/pdtFeederDetail/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= pdtFeederDetailService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/pdtFeederDetail/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        PdtFeederDetail model = pdtFeederDetailService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/pdtFeederDetail/save")
    public Result save(@RequestBody PdtFeederDetail pdtFeederDetail) {
        pdtFeederDetailService.saveOrUpdate(pdtFeederDetail);
        return Result.succeed("保存成功");
    }

    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/pdtFeederDetail/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<PdtFeederDetail>> map) {
        List<PdtFeederDetail> models = map.get("models");
        pdtFeederDetailService.saveOrUpdateBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/pdtFeederDetail/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("ids");
        pdtFeederDetailService.removeByIds(ids);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/pdtFeederDetail/leadIn")
    public  Result leadIn(MultipartFile excel,String createName) throws Exception {
        return pdtFeederDetailService.leadIn(excel,createName);
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/pdtFeederDetail/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<PdtFeederDetailVO> pdtFeederDetailList =pdtFeederDetailService.getFeederDetailList(ids);
        String feederName ="";
        if (pdtFeederDetailList != null) {
            feederName=pdtFeederDetailList.get(0).getFeederName();
        }
        //导出操作
        EasyPoiExcelUtil.exportExcel(pdtFeederDetailList, "料站表详情导出", "料站表详情导出", PdtFeederDetailVO.class, feederName+".xls", response);
    }
}
